﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsApp1.Common;
using static WinFormsApp1.Leo.week02.SlaughtrerdShow;

namespace WinFormsApp1.Leo.week02
{
    //分割领用列表
    public partial class SplitRequisShow : Form
    {
        public SplitRequisShow()
        {
            InitializeComponent();
        }


        private async void SplitRequisShow_Load(object sender, EventArgs e)
        {
            var items = await HttpClientHelper.RequestUrl<string, ApiResult<List<SplitRequisres>>>(URLCommon.Url1, "/WeekListOne_Leo/GetSplitRequisShow", HttpClientHelper.MethodType.GET, default);
            this.dataGridView1.DataSource = items.Data;
        }

        public class SplitRequisres
        {
            public int SplitRequisId { get; set; }

            /// <summary>
            /// 称重时间W
            /// </summary>
            public DateTime? SplitRequisTime { get; set; }

            /// <summary>
            /// 仓库
            /// </summary>
            public int DepotId { get; set; }

            /// <summary>
            /// 商品批号
            /// </summary>
            public string ProductName { get; set; }

            /// <summary>
            /// 商品代号
            /// </summary>
            public string UPCDataName { get; set; }

            /// <summary>
            /// 件数
            /// </summary>
            public string SplitRequisNum { get; set; }

            /// <summary>
            /// 毛重
            /// </summary>
            public string SplitRequisGrossWeight { get; set; }

            /// <summary>
            /// 皮重
            /// </summary>
            public string SplitRequisTares { get; set; }

            /// <summary>
            /// 净重
            /// </summary>
            public string SplitRequisWeight { get; set; }

            /// <summary>
            /// 备注
            /// </summary>
            public string SplitRequisRemark { get; set; }

        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, this.dataGridView1.RowHeadersWidth - 4, e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), this.dataGridView1.RowHeadersDefaultCellStyle.Font, rectangle, this.dataGridView1.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            {
                if (this.dataGridView1.Columns[e.ColumnIndex].HeaderText == "操作")
                {
                    StringFormat sf = StringFormat.GenericDefault.Clone() as StringFormat;//设置重绘单元格样式
                    sf.FormatFlags = StringFormatFlags.DisplayFormatControl;
                    sf.Alignment = StringAlignment.Center;
                    sf.LineAlignment = StringAlignment.Center;
                    sf.Trimming = StringTrimming.EllipsisCharacter;

                    e.PaintBackground(e.CellBounds, false);//重绘边框

                    //设置写入字体大小
                    System.Drawing.Font myFont = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                    SizeF sizeDel = e.Graphics.MeasureString("删除", myFont);
                    SizeF sizeMod = e.Graphics.MeasureString("修改", myFont);

                    float FDel = sizeDel.Width / (sizeDel.Width + sizeMod.Width);
                    float FMod = sizeMod.Width / (sizeDel.Width + sizeMod.Width);

                    //设置每个“按钮的边界”
                    RectangleF recDel = new RectangleF(e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Width * FDel, e.CellBounds.Height);
                    RectangleF recMod = new RectangleF(recDel.Right, e.CellBounds.Top, e.CellBounds.Width * FMod, e.CellBounds.Height);
                    e.Graphics.DrawString("删除", myFont, Brushes.Black, recDel, sf);
                    e.Graphics.DrawString("修改", myFont, Brushes.Black, recMod, sf);
                    e.Handled = true;

                }
            }
        }

        public record SplitRequisDels(int SplitRequisId);

        private async void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            {
                Point curPosition = e.Location;
                if (this.dataGridView1.Columns[e.ColumnIndex].HeaderText == "操作")
                {
                    Graphics g = this.dataGridView1.CreateGraphics();
                    System.Drawing.Font myFont = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                    SizeF sizeDel = g.MeasureString("删除", myFont);
                    SizeF sizeMod = g.MeasureString("修改", myFont);
                    float FDel = sizeDel.Width / (sizeDel.Width + sizeMod.Width);
                    float FMod = sizeMod.Width / (sizeDel.Width + sizeMod.Width);

                    Rectangle rectTotal = new Rectangle(0, 0, this.dataGridView1.Columns[e.ColumnIndex].Width, this.dataGridView1.Rows[e.RowIndex].Height);
                    RectangleF rectDel = new RectangleF(rectTotal.Left, rectTotal.Top, rectTotal.Width * FDel, rectTotal.Height);
                    RectangleF rectMod = new RectangleF(rectDel.Right, rectTotal.Top, rectTotal.Width * FMod, rectTotal.Height);

                    if (rectDel.Contains(curPosition))
                    {
                        if (dataGridView1.Columns[e.ColumnIndex].Name == "Clas" && e.RowIndex >= 0)
                        {
                            if (MessageBox.Show("您确定要删除吗？", "此删除为逻辑删除", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes)
                            {
                                int SlaughteredId = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["序列"].Value);

                                SplitRequisDels slaughteredDel = new(SlaughteredId);

                                var items = await HttpClientHelper.RequestUrl<SplitRequisDels, ApiResult<List<SplitRequisDel>>>(URLCommon.Url1, "/WorkList01_leo/GetSplitRequisDel", HttpClientHelper.MethodType.POST, slaughteredDel);

                                if (items.Code == 1)
                                {
                                    MessageBox.Show("删除成功");
                                }
                                else
                                {
                                    MessageBox.Show("删除失败");
                                }
                            }

                        }

                    }
                    else
                    if (rectMod.Contains(curPosition))
                    {

                        if (e.RowIndex >= 0)
                        {
                            int SplitRequisId = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["序列"].Value);

                            SplitRequisUpdate splitRequisUpdate = new(SplitRequisId);
                            splitRequisUpdate.Show();
                        }

                    }

                }
            }
        }

        public class SplitRequisDel
        {
            public int SplitRequisId { get; set; }
        }
    }
}
